<div>
	<table id="tblDevelopment" class="layui-hide" lay-filter="tblDevelopment"></table>
</div>
<script type="text/html" id="barDevelopment">
	<a class="layui-btn layui-btn-normal" lay-event="add">制定开发计划</a>
    <a class="layui-btn " lay-event="edit">编辑开发计划</a>
	<a class="layui-btn layui-btn-warm" lay-event="develop_success">开发成功</a>
	<a class="layui-btn layui-btn-danger" lay-event="develop_fail">开发失败</a>
</script>
<script>
	var row = null
	var table = null
	layui.use(['table', 'jquery', 'layer'], function() {
		var layer = layui.layer;
		table = layui.table;
		var $ = layui.jquery;
		//执行表格渲染
		table.render({
			elem: '#tblDevelopment', //指定原始表格元素选择器（推荐id选择器）
			url: "http://localhost:8080/chance/getChanceWithPlan",
			height: 'full-150', //容器高度
			cols: [
					[{
							checkbox: true
						},
						{
							field: 'chNo',
							title: '销售机会编号',
							width: 100
						},
						
						{
							field: 'chName',
							title: '客户名称'
						},
						{
							field: 'plNo',
							title: '计划编号',
							width: 100
						},
						{
							field: 'plContent',
							title: '开发计划'
						},
						{
							field: 'plDate',
							title: '开发日期'
						},
						{
							field: 'plState',
							title: '开发状态',
							templet: function(row) {
								switch (row.plState) {
									 
				
									case 0:
									return "开发中";
									case 1:
									return "开发成功";
									case 2:
									return "开发失败";
									default:
									return "未开发";
								}
							}
						}

					]
				] //设置表头
				,

			page: true,
			limits: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50],
			limit: 5,
			toolbar: '#barDevelopment', //设置整个表格的工具栏
			defaultToolbar: []
		});
		//工具栏事件监听
		table.on('toolbar(tblDevelopment)', function(obj) {
			var checkStatus = table.checkStatus(obj.config.id);
			
			//obj.event对应工具栏按钮的lay-event属性
			switch (obj.event) {
				case 'add':
					if(checkStatus.data.length<=0){
						layer.msg("请选择要制定计划的客户")
					}else{
						if(checkStatus.data[0].plState == 0 || checkStatus.data[0].plState == 1
						|| checkStatus.data[0].plState == 2){
							layer.msg("该客户已有开发计划")
						}else{
							// layer.msg('添加');
							row = checkStatus.data[0]
							layer.open({
								type: 2 //此处以iframe举例
									,
								title: '制定开发计划',
								area: ['400px', '450px'],
								shade: 0.3 //背景透明度，0是完全透明
									,
								maxmin: true //显示最大化和最小化按钮
									,
								offset: [ //窗口位置，居中显示
									($(window).height() - 450) / 2, ($(window).width() - 400) / 2
								],
								content: 'development/developmentAddPlan.html	' //窗口里面显示的页面
							});
						}
					}
					break;
				case 'edit':
					if (checkStatus.data.length <= 0) {
						layer.msg("请选择要编辑的对象")
					}else if(checkStatus.data[0].plState !=0 && checkStatus.data[0].plState !=1 && checkStatus.data[0].plState !=2){
						layer.msg("请先制定开发计划")
					}
					 else {
						row = checkStatus.data[0]
						layer.open({
							type: 2 //此处以iframe举例
								,
							title: '编辑开发计划',
							area: ['400px', '450px'],
							shade: 0.3 //背景透明度，0是完全透明
								,
							maxmin: true //显示最大化和最小化按钮
								,
							offset: [ //窗口位置，居中显示
								($(window).height() - 450) / 2, ($(window).width() - 400) / 2
							],
							content: 'development/developmentedit.html	' //窗口里面显示的页面
						});
					}
					break;
				case 'search':
					var key = $('#cusName').val()
					table.reload('tblDevelopment', {
						where: {
							cusName: key //查询条件
						},
						page: {
							curr: 1 //重新从第一页开始
						}
					})
					break;
				case 'develop_success':
					if (checkStatus.data.length <= 0) {
					 	layer.msg("请选择要开发成功的计划")
					 } 
					 else if(checkStatus.data[0].plState == 1 || checkStatus.data[0].plState == 2){
						layer.msg("开发计划已经执行完成")
					 }else if(checkStatus.data[0].plState != 1 && checkStatus.data[0].plState != 2 && checkStatus.data[0].plState != 0){
						layer.msg("请先制定开发计划")
					 }else {
					 	layer.confirm('确认开发成功吗?', function(index){
						var plan = checkStatus.data[0]
						plan.plState = 1 
					 	  $.ajax({
					 	  	contentType: "application/json;charset=utf-8",
					 	  	data:plan,
					 	  	dataType:'json',
					 	  	url:'http://localhost:8080/plan/developSuccess',
					 	  	Type:'POST',
					 	  	success:function(backjson){
					 	  		table.reload('tblDevelopment')
								layer.msg(backjson.msg)
					 	  	}
					 	  })
					 	  layer.close(index);
					 	}); 
					 }
					break;
				case 'develop_fail':
					if (checkStatus.data.length <= 0) {
					 	layer.msg("请选择要开发失败的计划")
					 } else if(checkStatus.data[0].plState == 1 || checkStatus.data[0].plState == 2){
						layer.msg("开发计划已经执行完成")
					 }else if(checkStatus.data[0].plState != 1 && checkStatus.data[0].plState != 2 && checkStatus.data[0].plState != 0){
						layer.msg("请先制定开发计划")
					 }else {
					 	layer.confirm('确认开发失败吗?', function(index){
						var plan = checkStatus.data[0]
						plan.plState = 2 
					 	  $.ajax({
					 	  	contentType: "application/json;charset=utf-8",
					 	  	data:plan,
					 	  	dataType:'json',
					 	  	url:'http://localhost:8080/plan/developFail',
					 	  	Type:'POST',
					 	  	success:function(backjson){
					 	  		table.reload('tblDevelopment')
								layer.msg(backjson.msg)
					 	  	}
					 	  })
					 	  layer.close(index);
					 	}); 
					 }
					break;
			};
		});
	})
</script>
